import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Vue extends JFrame{
	
	private JMenuBar Menu;
	private JMenu filemenu;
	
	private JMenuItem conf1;
	private JMenuItem conf2;
	private JMenuItem conf3;
	private JMenuItem conf4;
	private JMenuItem conf5;
	
	private JTextField CallPutFlag = new JTextField();
	private JTextField S = new JTextField();
	private JTextField X = new JTextField();
	private JTextField T = new JTextField();
	private JTextField r = new JTextField();
	private JTextField b = new JTextField();
	private JTextField V = new JTextField();
	private JTextField nStep = new JTextField();
	private JTextField nSimulation = new JTextField();
	private JTextField thread = new JTextField();
	private JTextField timeText = new JTextField();
	private JTextField resultText = new JTextField();
	
	private JLabel CallPutFlagLabel = new JLabel("CallPutFlag (c or p)");
	private JLabel SLabel = new JLabel("S");
	private JLabel XLabel = new JLabel("X");
	private JLabel TLabel = new JLabel("T");
	private JLabel rLabel = new JLabel("r");
	private JLabel bLabel = new JLabel("b");
	private JLabel VLabel = new JLabel("V");
	private JLabel nStepLabel = new JLabel("nStep");
	private JLabel nSimulationLabel = new JLabel("nSimulation");
	private JLabel threadLabel = new JLabel("Single or Multi (S or M)");
	private JLabel timeLabel = new JLabel("Time");
	private JLabel resultLabel = new JLabel("Result");
	
	private JButton submit = new JButton("Envoyer");
	
	private double result;
	private MonteCarlo monteCarlo;
	private double time;
	
	public Vue()
	{
		super("MonteCarlo Simulation");
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		
		Menu = new JMenuBar();
		filemenu = new JMenu("Simulation ?");
		
		conf1 = new JMenuItem("Simulation 1");
		conf2 = new JMenuItem("Simulation 2");
		conf3 = new JMenuItem("Simulation 3");
		conf4 = new JMenuItem("Simulation 4");
		conf5 = new JMenuItem("Simulation 5");
		
		conf1.addActionListener(new ActionListener() {
		      public void actionPerformed(ActionEvent e) {
		    	  lecture("simulation1.txt");
		      }
		      });
		
		conf2.addActionListener(new ActionListener() {
		      public void actionPerformed(ActionEvent e) {
		    	  lecture("simulation2.txt");
		      }
		      });
		
		conf3.addActionListener(new ActionListener() {
		      public void actionPerformed(ActionEvent e) {
		    	  lecture("simulation3.txt");
		      }
		      });
		
		conf4.addActionListener(new ActionListener() {
		      public void actionPerformed(ActionEvent e) {
		    	  lecture("simulation4.txt");
		      }
		      });
		
		conf5.addActionListener(new ActionListener() {
		      public void actionPerformed(ActionEvent e) {
		    	  lecture("simulation5.txt");
		      }
		      });
		
		filemenu.add(conf1);
		filemenu.add(conf2);
		filemenu.add(conf3);
		filemenu.add(conf4);
		filemenu.add(conf5);
		
		Menu.add(filemenu);
		this.setJMenuBar(Menu);
 
		JPanel jp  = new JPanel();
		jp.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
	     CallPutFlag.setSize(50, 50);
		this.add(jp);
		gbc.insets = new Insets(10, 10, 10, 10);
		gbc.weightx = 1;
		gbc.weighty = 1;
		
		
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		
		jp.add(CallPutFlagLabel, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		jp.add(CallPutFlag, gbc);
		gbc.fill = GridBagConstraints.CENTER;
		
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		jp.add(SLabel, gbc);
		
		gbc.gridx = 1;
		gbc.gridy = 2;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		jp.add(S, gbc);
		gbc.fill = GridBagConstraints.CENTER;

		gbc.gridx = 2;
		gbc.gridy = 1;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		jp.add(XLabel, gbc);
		
		gbc.gridx = 2;
		gbc.gridy = 2;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		jp.add(X, gbc);
		gbc.fill = GridBagConstraints.CENTER;
		
		gbc.gridx = 0;
		gbc.gridy = 4;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		jp.add(TLabel, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 5;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		jp.add(T, gbc);
		gbc.fill = GridBagConstraints.CENTER;
		
		gbc.gridx = 1;
		gbc.gridy = 4;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		jp.add(rLabel, gbc);
		
		gbc.gridx = 1;
		gbc.gridy = 5;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		jp.add(r, gbc);
		gbc.fill = GridBagConstraints.CENTER;
		
		
		gbc.gridx = 2;
		gbc.gridy = 4;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		jp.add(bLabel, gbc);
		
		gbc.gridx = 2;
		gbc.gridy = 5;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		jp.add(b, gbc);
		gbc.fill = GridBagConstraints.CENTER;
		
		gbc.gridx = 0;
		gbc.gridy = 7;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		jp.add(VLabel, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 8;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		jp.add(V, gbc);
		gbc.fill = GridBagConstraints.CENTER;
		
		gbc.gridx = 1;
		gbc.gridy = 7;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		jp.add(nStepLabel, gbc);
		
		gbc.gridx = 1;
		gbc.gridy = 8;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		jp.add(nStep, gbc);
		gbc.fill = GridBagConstraints.CENTER;
		
		gbc.gridx = 2;
		gbc.gridy = 7;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		jp.add(nSimulationLabel, gbc);
		
		gbc.gridx = 2;
		gbc.gridy = 8;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		jp.add(nSimulation, gbc);
		gbc.fill = GridBagConstraints.CENTER;
		
		gbc.gridx = 0;
		gbc.gridy = 10;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		
		jp.add(threadLabel, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 11;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		jp.add(thread, gbc);
		gbc.fill = GridBagConstraints.CENTER;
		
		gbc.gridx = 1;
		gbc.gridy = 10;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		
		jp.add(timeLabel, gbc);
		
		gbc.gridx = 1;
		gbc.gridy = 11;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		jp.add(timeText, gbc);
		gbc.fill = GridBagConstraints.CENTER;
		
		gbc.gridx = 2;
		gbc.gridy = 10;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		
		jp.add(resultLabel, gbc);
		
		gbc.gridx = 2;
		gbc.gridy = 11;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		jp.add(resultText, gbc);
		gbc.fill = GridBagConstraints.CENTER;
		
		gbc.gridx = 2;
		gbc.gridy = 13;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		jp.add(submit, gbc);
		
		submit.addActionListener(new ActionListener() {
		      public void actionPerformed(ActionEvent e) {
		    	
		    	  if (thread.getText().equals("S"))
		    	  {
		    		  monteCarlo = new SimpleThread();
		    		
		    		  time = 0;
		    		  time -= System.currentTimeMillis();
		    		  result = monteCarlo.monteCarloStandardOption(

		    				  CallPutFlag.getText().charAt(0),
		    				  Double.parseDouble(S.getText()),
		    				  Double.parseDouble(X.getText()),
		    				  Double.parseDouble(T.getText()),
		    				  Double.parseDouble(r.getText()),
		    				  Double.parseDouble(b.getText()),
		    				  Double.parseDouble(V.getText()),
		    				  Integer.parseInt(nStep.getText()),
		    				  Integer.parseInt(nSimulation.getText())
		    				  );
		    		  time += System.currentTimeMillis();
		    		  timeText.setText(Double.valueOf(time).toString() + " ms");
		    		  resultText.setText(Double.valueOf(result).toString());
		    		  
		    		  System.out.println("Result : " +result +"\nTime : " +time + " ms");
		    	      
		    	  }
		    	  if (thread.getText().equals("M"))
		    	  {
		    		  monteCarlo = new MultiThread();
		    		
		    		  time = 0;
		    		  time -= System.currentTimeMillis();
		    		  result = monteCarlo.monteCarloStandardOption(

		    				  CallPutFlag.getText().charAt(0),
		    				  Double.parseDouble(S.getText()),
		    				  Double.parseDouble(X.getText()),
		    				  Double.parseDouble(T.getText()),
		    				  Double.parseDouble(r.getText()),
		    				  Double.parseDouble(b.getText()),
		    				  Double.parseDouble(V.getText()),
		    				  Integer.parseInt(nStep.getText()),
		    				  Integer.parseInt(nSimulation.getText())
		    				  );
		    		  time += System.currentTimeMillis();
		    		  timeText.setText(Double.valueOf(time).toString() + " ms");
		    		  resultText.setText(Double.valueOf(result).toString());
		    		  
		    		 
		    		  System.out.println("Result : " +result +"\nTime : " +time + " ms");
		    		  
		    	  }
		      }
		    });
		
		
		this.pack();
		this.setSize(600,400);
		this.setLocationRelativeTo(null);
		this.setVisible(true);

		
	}
	public void lecture(String fichier)
	{
		
		String filePath = fichier;
		 
		Scanner scanner = null;
		try {
			scanner = new Scanner(new File(filePath));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		CallPutFlag.setText(scanner.nextLine());
		S.setText(scanner.nextLine());
		X.setText(scanner.nextLine());
		T.setText(scanner.nextLine());
		r.setText(scanner.nextLine());
		b.setText(scanner.nextLine());
		V.setText(scanner.nextLine());
		nStep.setText(scanner.nextLine());
		nSimulation.setText(scanner.nextLine());
		thread.setText(scanner.nextLine());	 
		scanner.close();
	}
	
}
